package cn.hn.monkey.stock;

import static cn.hn.monkey.stock.tools.WhatHandler.REFRESH_HANDLE_TIPS;
import static cn.hn.monkey.stock.tools.WhatHandler.REFRESH_LOOP;
import static cn.hn.monkey.stock.tools.WhatHandler.REFRESH_STOCK;
import static cn.hn.monkey.stock.tools.WhatHandler.REFRESH_SH_TIPS;
import static cn.hn.monkey.stock.tools.WhatHandler.REFRESH_SZ_TIPS;
import static cn.hn.monkey.stock.tools.WhatHandler.REFRESH_WAIT;

import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

import cn.hn.monkey.stock.data.DaysData;
import cn.hn.monkey.stock.data.SaveData;

public class CoreActivity extends BaseActivity {

    BaseApplication app;
    TextView tvStockCode, tvStockName, tvStockPrice, tvStockDelta, tvStockOther, tvMessage;
    List<DaysData.DayDataStock> dayDataStocksList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_core);
        app = (BaseApplication) getApplication();
        app.refreshTodayData(handler);
        initView();
        initData();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        app.stopRefreshTodayData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        app.destroy();
    }

    void initView() {
        tvStockCode = findViewById(R.id.tv_stock_code);
        tvStockName = findViewById(R.id.tv_stock_name);
        tvStockPrice = findViewById(R.id.tv_stock_price);
        tvStockDelta = findViewById(R.id.tv_stock_delta);
        tvStockOther = findViewById(R.id.tv_stock_other);
        tvMessage = findViewById(R.id.tv_message);
    }

    void initData() {
        dayDataStocksList = app.getStockList(88);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            SaveData.StockDetail sd;
            float delta;
            Resources res = getResources();
            switch(msg.what) {
                case REFRESH_WAIT:
                    tvMessage.setText("Wait:" + msg.arg1);
                    break;
                case REFRESH_LOOP:
                    sd = (SaveData.StockDetail) msg.obj;
                    tvStockCode.setText(sd.symbol);
                    tvStockCode.setTextColor(res.getColor(R.color.gray));
                    tvStockName.setText(sd.name);
                    tvStockName.setTextColor(res.getColor(R.color.white));
                    tvStockPrice.setText(String.format("%8.2f", sd.trade));
                    delta = sd.trade - sd.settlement;
                    tvStockPrice.setTextColor(delta > 0.009 ? res.getColor(R.color.red) : (delta < 0.009 ? res.getColor(R.color.green) : res.getColor(R.color.gray)));
                    tvStockOther.setText(String.format("{%d/%d}", msg.arg1, msg.arg2));
                    tvStockOther.setGravity(Gravity.RIGHT);
                    break;
                case REFRESH_STOCK:
                    sd = (SaveData.StockDetail) msg.obj;
                    tvStockCode.setText(sd.symbol);
                    tvStockCode.setTextColor(res.getColor(R.color.cyan));
                    tvStockName.setText(sd.name);
                    tvStockName.setTextColor(res.getColor(R.color.yellow));
                    delta = sd.trade - sd.settlement;
                    tvStockPrice.setText(String.format("%8.2f %c%.2f%c", sd.trade, delta > 0.009 ? '+' : ' ', sd.changepercent, '%'));
                    tvStockPrice.setTextColor(delta > 0.009 ? res.getColor(R.color.red) : (delta < 0.009 ? res.getColor(R.color.green) : res.getColor(R.color.gray)));
                    tvStockDelta.setText(String.format("%d", msg.arg1));
                    tvStockDelta.setGravity(Gravity.LEFT);
                    tvStockOther.setText("");
                    break;
                case REFRESH_SH_TIPS:
                    tvMessage.setText("刷新上海市场行情");
                    break;
                case REFRESH_SZ_TIPS:
                    tvMessage.setText("刷新深圳市场行情");
                    break;
                case REFRESH_HANDLE_TIPS:
                    tvMessage.setText("处理数据");
                    tvStockCode.setText("");
                    tvStockName.setText("");
                    tvStockPrice.setText("");
                    tvStockDelta.setText("");
                    tvStockOther.setText("");
                    break;
            }
        }
    };
}
